package com.lyubia.shop.manager.controller;

import com.lyubia.shop.common.result.BaseResult;
import com.lyubia.shop.manager.pojo.GoodsCategory;
import com.lyubia.shop.manager.service.GoodsCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品分类管理
 */
@Controller
@RequestMapping("/goods/category")
public class GoodsCategoryController {

    @Resource
    private GoodsCategoryService goodsCategoryService;

    /**
     * 前往商品分类页
     * 查询全部三级分类
     * @return 页面
     */
    @GetMapping("/index")
    public String index(Model model) {
        List<GoodsCategory> categoryList = goodsCategoryService.selectCategoryAndChild();
        model.addAttribute("categoryList",categoryList);
        return "goods/category/goods_category_list";
    }

    /**
     * 前往新增页面
     * @return 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model) {
        // 查询一级分类
        List<GoodsCategory> goodsCategories = goodsCategoryService.selectTopCategory();
        model.addAttribute("gcList",goodsCategories);
        return "goods/category/goods_category_add";
    }

    /**
     * 根据parentId查询子集分类
     * @param parentId 父id
     * @return 列表
     */
    @PostMapping("/selectCategoryByParentId/{parentId}")
    @ResponseBody
    public List<GoodsCategory> selectCategoryByParentId(@PathVariable("parentId") Short parentId) {
        return goodsCategoryService.selectCategoryByParentId(parentId);
    }

    /**
     * 保存商品分类
     * @param goodsCategory 商品分类信息
     * @return 结果集
     */
    @PostMapping("/save")
    @ResponseBody
    public BaseResult saveCategory(GoodsCategory goodsCategory) {
        return goodsCategoryService.saveGoodsCategory(goodsCategory);
    }

    /**
     * 前往修改页面
     * @return 修改
     */
    @GetMapping("/toExit/{id}")
    public String toExit(@PathVariable Short id, Model model) {
        GoodsCategory goodsCategory = goodsCategoryService.selectCategoryById(id);
        List<GoodsCategory> parentList = goodsCategoryService.selectTopCategory();
        model.addAttribute("goodsCategory",goodsCategory);
        model.addAttribute("parentList",parentList);
        return "goods/category/goods_category_exit";
    }

    /**
     * 修改商品分类
     * @param goodsCategory 商品分类信息
     * @return 结果集
     */
    @PostMapping("/exit")
    @ResponseBody
    public BaseResult exitGoodsCategory(GoodsCategory goodsCategory) {
        return goodsCategoryService.exitGoodsCategory(goodsCategory);
    }

    /**
     * 根据id删除商品分类
     * @param id id
     * @return 结果集
     */
    @PostMapping("/del/{id}")
    @ResponseBody
    public BaseResult delCategoryById(@PathVariable Short id) {
        return goodsCategoryService.delCategoryById(id);
    }

}
